package com.ruoyi.production.service.impl.carton;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.production.domain.carton.CartonAmount;
import com.ruoyi.production.mapper.carton.CartonAmountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.carton.CartonPurchaseMapper;
import com.ruoyi.production.domain.carton.CartonPurchase;
import com.ruoyi.production.service.carton.ICartonPurchaseService;

/**
 * 纸箱购买信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-10
 */
@Service
public class CartonPurchaseServiceImpl implements ICartonPurchaseService
{
    @Autowired
    private CartonPurchaseMapper cartonPurchaseMapper;
    @Autowired
    private CartonAmountMapper cartonAmountMapper;

    /**
     * 查询纸箱购买信息
     *
     * @param cartonPurchaseId 纸箱购买信息ID
     * @return 纸箱购买信息
     */
    @Override
    public CartonPurchase selectCartonPurchaseById(Long cartonPurchaseId)
    {
        return cartonPurchaseMapper.selectCartonPurchaseById(cartonPurchaseId);
    }

    /**
     * 查询纸箱购买信息列表
     *
     * @param cartonPurchase 纸箱购买信息
     * @return 纸箱购买信息
     */
    @Override
    public List<CartonPurchase> selectCartonPurchaseList(CartonPurchase cartonPurchase)
    {
        return cartonPurchaseMapper.selectCartonPurchaseList(cartonPurchase);
    }

    /**
     * 新增纸箱购买信息
     *
     * @param cartonPurchase 纸箱购买信息
     * @return 结果
     */
    @Override
    public int insertCartonPurchase(CartonPurchase cartonPurchase)
    {
        return cartonPurchaseMapper.insertCartonPurchase(cartonPurchase);
    }

    /**
     * 修改纸箱购买信息
     *
     * @param cartonPurchase 纸箱购买信息
     * @return 结果
     */
    @Override
    public int updateCartonPurchase(CartonPurchase cartonPurchase)
    {
        Long cartonPurchaseId = cartonPurchase.getCartonPurchaseId();
        //数据库存储的数据
        CartonPurchase PurchaseDB = selectCartonPurchaseById(cartonPurchaseId);
        //数据库存储的原数据的总量
        CartonAmount originAmounts = new CartonAmount();
        originAmounts.setCartonSpecification(PurchaseDB.getCartonSpecification());
        originAmounts.setCartonTypeId(PurchaseDB.getCartonTypeId());
        originAmounts.setOriginCompany(PurchaseDB.getOriginCompany());
        List<CartonAmount> amounts = cartonAmountMapper.selectCartonAmountList(originAmounts);
        CartonAmount originAmount = amounts.get(0);
        if (PurchaseDB.getOriginCompany().equals(cartonPurchase.getOriginCompany()) &&
                PurchaseDB.getCartonTypeId().equals(cartonPurchase.getCartonTypeId()) &&
                PurchaseDB.getCartonSpecification().equals(cartonPurchase.getCartonSpecification())
        ) {
            originAmount.setStorageAmount(originAmount.getStorageAmount()+cartonPurchase.getPurchaseAmount()-PurchaseDB.getPurchaseAmount());
            cartonAmountMapper.updateCartonAmount(originAmount);
        }else {
            CartonAmount newAmounts = new CartonAmount();
            newAmounts.setCartonSpecification(cartonPurchase.getCartonSpecification());
            newAmounts.setCartonTypeId(cartonPurchase.getCartonTypeId());
            newAmounts.setOriginCompany(cartonPurchase.getOriginCompany());
            List<CartonAmount> amounts1 = cartonAmountMapper.selectCartonAmountList(newAmounts);
            if (StringUtils.isEmpty(amounts1)){
                newAmounts.setStorageAmount(cartonPurchase.getPurchaseAmount());
                cartonAmountMapper.insertCartonAmount(newAmounts);
            }else {
                CartonAmount newAmount = amounts1.get(0);
                newAmount.setStorageAmount(newAmount.getStorageAmount()+cartonPurchase.getPurchaseAmount());
                cartonAmountMapper.updateCartonAmount(newAmount);
            }
            originAmount.setStorageAmount(originAmount.getStorageAmount()-PurchaseDB.getPurchaseAmount());
            cartonAmountMapper.updateCartonAmount(originAmount);
        }
        return cartonPurchaseMapper.updateCartonPurchase(cartonPurchase);
    }

    /**
     * 批量删除纸箱购买信息
     *
     * @param cartonPurchaseIds 需要删除的纸箱购买信息ID
     * @return 结果
     */
    @Override
    public int deleteCartonPurchaseByIds(Long[] cartonPurchaseIds)
    {
        for (Long cartonPurchaseId : cartonPurchaseIds) {
            CartonPurchase cartonPurchase = selectCartonPurchaseById(cartonPurchaseId);
            CartonAmount cartonAmount = new CartonAmount();
            cartonAmount.setCartonSpecification(cartonPurchase.getCartonSpecification());
            cartonAmount.setOriginCompany(cartonPurchase.getOriginCompany());
            cartonAmount.setCartonTypeId(cartonPurchase.getCartonTypeId());
            List<CartonAmount> amounts = cartonAmountMapper.selectCartonAmountList(cartonAmount);
            CartonAmount cartonAmount1 = amounts.get(0);
            cartonAmount1.setStorageAmount(cartonAmount1.getStorageAmount() - cartonPurchase.getPurchaseAmount());
            cartonAmountMapper.updateCartonAmount(cartonAmount1);
        }
        return cartonPurchaseMapper.deleteCartonPurchaseByIds(cartonPurchaseIds);
    }

    /**
     * 删除纸箱购买信息信息
     *
     * @param cartonPurchaseId 纸箱购买信息ID
     * @return 结果
     */
    @Override
    public int deleteCartonPurchaseById(Long cartonPurchaseId)
    {
        CartonPurchase cartonPurchase = selectCartonPurchaseById(cartonPurchaseId);
        CartonAmount cartonAmount = new CartonAmount();
        cartonAmount.setCartonSpecification(cartonPurchase.getCartonSpecification());
        cartonAmount.setCartonTypeId(cartonPurchase.getCartonTypeId());
        cartonAmount.setOriginCompany(cartonPurchase.getOriginCompany());
        List<CartonAmount> amounts = cartonAmountMapper.selectCartonAmountList(cartonAmount);
        CartonAmount cartonAmount1 = amounts.get(0);
        cartonAmount1.setStorageAmount(cartonAmount1.getStorageAmount() - cartonPurchase.getPurchaseAmount());
        cartonAmountMapper.updateCartonAmount(cartonAmount1);
        return cartonPurchaseMapper.deleteCartonPurchaseById(cartonPurchaseId);
    }

    /**
     * 插入种子购买表更新种子量表
     *
     * @param cartonPurchase
     * @return
     */
    @Override
    public int insertPurchaseUpdateAmount(CartonPurchase cartonPurchase) {
        CartonAmount cartonAmount = new CartonAmount();
        cartonAmount.setCreateTime(cartonPurchase.getCreateTime());
        cartonAmount.setCreateBy(cartonPurchase.getCreateBy());
        cartonAmount.setUpdateTime(cartonPurchase.getUpdateTime());
        cartonAmount.setUpdateBy(cartonPurchase.getUpdateBy());
        cartonAmount.setCartonSpecification(cartonPurchase.getCartonSpecification());
        cartonAmount.setCartonTypeId(cartonPurchase.getCartonTypeId());
        cartonAmount.setOriginCompany(cartonPurchase.getOriginCompany());
        List<CartonAmount> cartonAmounts = cartonAmountMapper.selectCartonAmountList(cartonAmount);
        cartonAmount.setStorageAmount(cartonPurchase.getPurchaseAmount());
        if (!StringUtils.isEmpty(cartonAmounts)) {
            CartonAmount amount = cartonAmounts.get(0);
            amount.setStorageAmount(amount.getStorageAmount() + cartonPurchase.getPurchaseAmount());
            cartonAmountMapper.updateCartonAmount(amount);
        } else {
            cartonAmountMapper.insertCartonAmount(cartonAmount);
        }
        return insertCartonPurchase(cartonPurchase);
    }
}
